<?php
namespace Common\Model;

class CustomerModel extends CommonModel{
	protected $_validate = array(
			array('nickname','require','请输入员工昵称'),
			array('username','require','请输入用户名'),
			array('username','/^[a-zA-Z0-9]{5,15}$/','用户名限5~15个字母或数字组合'),
			array('username','','该用户名已存在',0,'unique'),
			array('password','require','请输入密码'),
			array('username','/^[a-zA-Z0-9]{5,15}$/','密码限5~15个字母或数字组合'),
	);
    /**
     * 获取access_token
     */
    public function getToken($customer_id = 1,&$appid = null){
        
    	$config = $this->where(array('id'=>$customer_id))->find();
    	
        if(empty($config['appid']) || empty($config['appsecret'])){
            $this->error = '项目未配置appid或appsecret';
            return false;
        }
        if($config && $config['expire_time'] > time() && !empty($config['access_token'])){//未过期
            return $config['access_token'];
        }else{
        	$appid = $config['appid'];
        	$appsecret = $config['appsecret'];
            $jsondata = D('Api')->getAccessToken($appid,$appsecret);
            if(empty($jsondata['access_token'])){
                $this->error = 'access_token获取失败';
                return false;
            }else{
                $time = time()+7000;
                $this->save(array('id'=>$customer_id,'access_token'=>$jsondata['access_token'],'expire_time'=>$time));
                return $jsondata['access_token'];
            }
        }
    }
    
}
